﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using BLL;
namespace Web.Admin 
{
    public partial class ArticleClassAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断当前角色对象是否有相应的权限
            Role role = Session["role"] as Role;//从session取角色对象
            if (role != null && role.Authority != null && !role.Authority.Contains("ArticleClassAdd"))
            {
                //没权限跳转到提示页面或首页
                Response.Redirect("/Admin/Default.aspx");
            }
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null && !IsPostBack)
            {//修改
                //根据id从数据库取数据
                ArticleClass o = ArticleClassBll.SelectOne(int.Parse(id.ToString()));
                //显示到界面
                this.TextBox_name.Text = o.name;
                this.TextBox_desc.Text = o.Desc;


            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //第一步获取数据
            string Name = this.TextBox_name.Text;
            string desc = this.TextBox_desc.Text;
            //第二步  创建实体类对想
            ArticleClass obj = new ArticleClass();
            obj.name = Name; 
            obj.Desc = desc;
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null)
            {//修改
                obj.Id = int.Parse(id.ToString());//id赋值
                bool r = ArticleClassBll.Update(obj);
                if (r)
                {//修改成功
                    Response.Redirect("/Admin/ArticleClassList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('修改失败')</script>");
                }
            }
            else//添加
            { 
                //第三步调用 业务类的添加方法
                bool r = ArticleClassBll.Add(obj);
                if (r)
                {//添加成功
                    Response.Redirect("/Admin/ArticleClassList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('添加失败')</script>");
                }
            }

        }
    }
}